﻿using HR.Config;
using HR.IService;
using HR.Models;

namespace HR.Service
{
    public class EngageResumeService : IEngageResumeService
    {
        private readonly HRDBContext db;

        public EngageResumeService(HRDBContext db)
        {
            this.db = db;
        }

        public Task<int> EngageResumeAdd(engage_resume er)
        {
            return Task.Run(() =>
            {
                db.engage_resumes.Add(er);
                return db.SaveChanges();
            });
        }

        public Task<List<engage_resume>> ERFiltrate(EngageResumeFiltrate erf)
        {
            return Task.Run(() =>
            {
                List<engage_resume> engage_resumes = db.engage_resumes.ToList();
                List<engage_resume> list = db.engage_resumes.ToList().Where(er => (
                er.human_major_kind_id.Trim() == erf.major_kind_id && er.human_major_id.Trim() == erf.major_id)
                && (er.human_name.Contains(erf.keyword) || er.human_telephone.Contains(erf.keyword)
                || er.human_idcard.Contains(erf.keyword) || er.human_history_records.Contains(erf.keyword))
                && (er.regist_time > erf.opendate && er.regist_time < erf.closedate)).ToList();
                //List<engage_resume> list = engage_resumes.Where(er => er.human_major_kind_id.Trim()== erf.major_kind_id).ToList();
                return list;
            });
        }
        public Task<List<engage_resume>> ERFiltrate2(EngageResumeFiltrate erf)
        {
            return Task.Run(() =>
            {
                List<engage_resume> engage_resumes = db.engage_resumes.ToList();
                List<engage_resume> list = db.engage_resumes.ToList().Where(er => (
                er.human_major_kind_id.Trim() == erf.major_kind_id && er.human_major_id.Trim() == erf.major_id)
                && (er.human_name.Contains(erf.keyword) || er.human_telephone.Contains(erf.keyword)
                || er.human_idcard.Contains(erf.keyword) || er.human_history_records.Contains(erf.keyword))
                && (er.regist_time > erf.opendate && er.regist_time < erf.closedate)
                && (!string.IsNullOrEmpty(er.recomandation))).ToList();
                //List<engage_resume> list = engage_resumes.Where(er => er.human_major_kind_id.Trim()== erf.major_kind_id).ToList();
                return list;
            });
        }

        public Task<int> EngageResumeUdp(engage_resume er)
        {
            return Task.Run(() =>
            {
                engage_resume? ere = db.engage_resumes.Find(er.res_id);
                ere.recomandation = er.recomandation;
                ere.human_major_kind_id = er.human_major_kind_id;
                ere.human_major_kind_name = er.human_major_kind_name;
                ere.human_major_id = er.human_major_id;
                ere.human_major_name = er.human_major_name;
                ere.human_name = er.human_name;
                ere.engage_type = er.engage_type;
                ere.human_address = er.human_address;
                ere.human_postcode = er.human_postcode;
                ere.human_sex = er.human_sex;
                ere.human_age = er.human_age;
                ere.human_birthday = er.human_birthday;
                ere.human_idcard = er.human_idcard;
                ere.human_history_records = er.human_history_records;
                ere.remark = er.remark;
                ere.human_birthplace = er.human_birthplace;
                ere.register = er.register;
                return db.SaveChanges();
            });
        }
        public Task<int> EngageResumeDel(short res_id)
        {
            return Task.Run(() =>
            {
                engage_resume? er = db.engage_resumes.Find(res_id);
                db.engage_resumes.Remove(er);
                return db.SaveChanges();
            });
        }

        //查询有效简历的集合
        public Task<Paging<engage_resume>> EngageResumeValidFill(int pgsize, int pgcurrent)
        {
            return Task.Run(() =>
            {
                List<engage_resume> list = db.engage_resumes.Where(er => string.IsNullOrEmpty(er.recomandation)).ToList();
                List<engage_resume> tabs = list.Skip(( pgcurrent - 1) * pgsize).Take(pgsize).ToList();
                Paging<engage_resume> pg = new Paging<engage_resume>()
                {
                    tabs = tabs,
                    rows = list.Count
                };
                return pg;
            });

        }
    }
}
